package org.sjs.dreamfly.biz.shortvideo

import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import org.sjs.dreamfly.R
import org.sjs.dreamfly.common.base.BaseActivity
import org.sjs.dreamfly.common.util.Router
import org.sjs.dreamfly.common.util.PhemonUtils
import org.sjs.dreamfly.model.VideoInfo
import org.sjs.dreamfly.player.IPlayerDataProvider
import org.sjs.dreamfly.player.PlayerFragment
import org.sjs.dreamfly.player.ShortVideoPlayerFragment

class ShortVideoActivity: BaseActivity(), IPlayerDataProvider {

    private lateinit var viewModel: ShortVideoViewModel
    private var playerFragment : ShortVideoPlayerFragment? = null

    override fun onInitViewModel() {
        viewModel = ViewModelProvider(PhemonUtils.getApplication())[ShortVideoViewModel::class.java]
    }

    override fun onLoadData(savedInstanceState: Bundle?, newIntent: Intent?) {
        if (playerFragment != null && playerFragment!!.isAdded) {
            return
        } else {
            playerFragment = PlayerFragment.newInstance(intent.getIntExtra(Router.KEY_INDEX, 0))
            playerFragment?.setDataProvider(this)
            supportFragmentManager.beginTransaction().add(R.id.container, playerFragment!!).commit()
        }
    }

    override fun onBindViews() {

    }

    override fun onInitViews() {
        hideTitleBar()
    }

    override suspend fun loadVideoList(): MutableList<VideoInfo> {
        return viewModel.getVideoList()
    }

    override fun getVideoList(): MutableList<VideoInfo> {
        return viewModel.getVideoList()
    }

    override fun loadMore(callback: () -> Unit?) {
        viewModel.loadMore(callback)
    }
}